package com.icehand.proxy.demo.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author icehand
 */
public class JdkDynamicProxy implements InvocationHandler {
    private Object target;

    public Object bind(Object target){
        this.target = target;
        //取得代理对象
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //可以在调用方法前后做一些操作
        System.out.println("this is jdk dynamic proxy");
        return method.invoke(target,args);
    }
}
